<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="Form" class="login-form" autocomplete="on" label-position="left" :rules="rules">

      <div class="title-container">
        <h3 class="title">后台系统</h3>
      </div>

      <el-form-item prop="userName" label="用户名">
        <el-input v-model="Form.userName" placeholder="用户名" tabindex="1" autocomplete="on" />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password" label="密 码">
          <el-input :key="passwordType" ref="password" v-model="Form.password" :type="passwordType"
            placeholder="Password" name="password" tabindex="2" autocomplete="on" @blur="capsTooltip = false"
            @keyup.enter="handleLogin" />
        </el-form-item>
      </el-tooltip>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:230px;" @click.prevent="handleLogin">
        登录</el-button>


    </el-form>


  </div>

</template>

<script lang="ts" setup>
import { login } from '@/api/user'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance, FormRules, useNamespace } from 'element-plus'
import { ElMessage } from 'element-plus'
// 创建路由实例
const route = useRouter()

const Form = ref({
  userName: 'admin',
  password: '123456'
})
const capsTooltip = ref({

})
const passwordType = ref<'password' | 'text'>('password')
const loading = ref(false)
const showDialog = ref(false)

function showPwd() {

}
const loginForm = ref<FormInstance>()
async function handleLogin() {
  await loginForm.value?.validate(res => {
    if (res) {
      login(Form.value).then(res => {
        console.log(res, '登录返回值');
        if (res.code == 1) {
          loading.value = true
          ElMessage({
            message: '登陆成功',
            type: 'success',
          })
          // 路由跳转
          route.push('/')
        } else {
          ElMessage({
            message: res.message,
            type: 'warning',
          })
        }
      }).finally(() => {
        loading.value = false
      })
    }

  })

}

// 表单验证
const rules = reactive<FormRules>({
  userName: [
    { message: '请输入名称', trigger: 'blur' },
  ],
  password: [
    { message: '请输入密码', trigger: 'blur' },
  ],


})

</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-plus css */
.login-container {
  form {
    margin: 0 auto;
  }

  .el-input {
    height: 47px;
    width: 85%;

    .el-input__wrapper,
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      box-shadow: none;
    }

    input {
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    display: flex;
    align-items: center;

    label {
      padding: 0;
      text-align: center;
      width: 60px;
      text-align: center;
    }
  }
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;


.login-container {
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: $bg;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
